#coding = UTF8
import web
import os,sys
PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.append(PATH)


urls = (
        '/','main',
        '/ip','ip',
        )

app = web.application(urls, globals(), autoreload = True)

class ip: # refresh ip
    def GET(self):
        try:
            ip = web.ctx.env['HTTP_REMOTE_HOST']
        except KeyError:
            ip = web.ctx.env['REMOTE_ADDR']
        f = open(PATH + '/ip.txt','w')
        f.write(ip)
        f.close()
        return ip
        
class main: # show stored ip
    def GET(self):
        f = open(PATH + '/ip.txt','r')
        ip = f.read()
        f.close()
        return ip
    
if __name__ == '__main__':
    app.run()
else:
    application = app.wsgifunc()
    